<template>
  <div class="fullcontainer">
    <div class="container" v-bind:class="{ 'right-panel-active': signup }">
      <div class="form-container sign-up-container">
        <register-form />
      </div>
      <div class="form-container sign-in-container">
        <el-form ref="loginform" :model="loginform" :rules="loginrules">
          <h1>登录</h1>
          <el-form-item prop="username">
            <el-input
              type="text"
              v-model="loginform.username"
              autofocus="true"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              v-model="loginform.password"
              placeholder="请输入用户密码"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              @click="handleLogin"
              class="btn"
              type="primary"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="overlay-container">
        <div class="overlay">
          <div class="overlay-panel overlay-left">
            <h1>已有账号？</h1>
            <p>请使用您的账号进行登录</p>
            <el-button
              class="login-btn"
              :disabled="disabled"
              type="primary"
              @click="signup = false"
              >登录</el-button
            >
          </div>
          <div class="overlay-panel overlay-right">
            <h1>没有账号?</h1>
            <p>立即注册加入我们，和我们一起开始旅程吧</p>

            <el-button class="login-btn" type="primary" @click="signup = true"
              >注册</el-button
            >
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import RegisterForm from "./register.vue";
export default {
  name: "Login",
  components: {
    RegisterForm
  },
  data() {
    return {
      disabled: false,
      loading: false,
      signup: false,
      loginform: {
        username: "",
        password: ""
      },

      loginrules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    handleLogin() {
      this.$refs.loginform.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginform)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.fullcontainer {
  background-image: linear-gradient(120deg, #3498db, #8e44ad);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.container {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  position: relative;
  overflow: hidden;
  width: 800px;
  max-width: 100%;
  min-height: 480px;
}
.container .el-form {
  background: #fff;
  display: flex;
  flex-direction: column;
  padding: 0 50px;
  height: 100%;
  justify-content: center;
  text-align: center;
}
.form-container {
  position: absolute;
  top: 0;
  height: 100%;
  transition: all 0.6s ease-in-out;
}
.overlay-container {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  overflow: hidden;
  transition: transform 0.6s ease-in-out;
  z-index: 100;
}
.btn {
  background: linear-gradient(120deg, #3498db, #8e44ad);
  background-size: auto;
  border: none;
  color: #fff;
  background-size: 200%;
  padding: 12px 45px;
  letter-spacing: 10px;
}
.form-container .el-button:active {
  transform: scale(0.95);
}
.form-container .el-button:hover {
  background-position: right;
}
.sign-in-container {
  left: 0;
  width: 50%;
  z-index: 2;
}
.login-btn {
  border-radius: 20px;
  background: transparent;
  border-color: #fff;
  font-size: 12px;
  padding: 12px 45px;
  letter-spacing: 10px;
}
.login-btn:hover {
  background-color: unset;
  border: 1px solid #fff;
}
.sign-up-container {
  left: 0;
  width: 50%;
  z-index: 1;
  opacity: 0;
}

.overlay {
  background-image: linear-gradient(120deg, #3498db, #8e44ad);
  color: #fff;
  position: relative;
  left: -100%;
  height: 100%;
  width: 200%;
  transform: translateY(0);
  transition: transform 0.6s ease-in-out;
}
.overlay-panel {
  position: absolute;
  top: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 0 40px;
  height: 100%;
  width: 50%;
  text-align: center;
  transform: translateY(0);
  transition: transform 0.6s ease-in-out;
}
.overlay-right {
  right: 0;
  transform: translateY(0);
}
.overlay-left {
  transform: translateY(-20%);
}

.container.right-panel-active .sign-in-container {
  transform: translateY(100%);
}

.container.container.right-panel-active .overlay-container {
  transform: translateX(-100%);
}
.container.right-panel-active .sign-up-container {
  transform: translateX(100%);
  opacity: 1;
  z-index: 5;
}
.container.container.right-panel-active .overlay {
  transform: translateX(50%);
}
.container.container.right-panel-active .overlay-left {
  transform: translateY(0);
}
.container.container.right-panel-active .overlay-right {
  transform: translateY(20%);
}
</style>
